﻿package
{
	import flash.display.*;
	import flash.events.*;
	import flash.xml.*;
	import flash.net.*;
	import flash.utils.*;
	
	public class UILayer extends MovieClip
	{
		private var UIItems:Object;
		
		private var FriendUIItems:Object;
		
		private var urlLoader:URLLoader;
		
		//add all UI items into movie clip, the coordinator comes from UI Configuration
		public function UILayer()
		{
			
			var webroot:String = "file://C:\\Work\\Game\\SimLife\\";
			var urlRequest:URLRequest = new URLRequest(webroot + "Configuration\\UIConfig.xml");
			urlLoader = new URLLoader();
			
			urlLoader.load(urlRequest);
			urlLoader.addEventListener(Event.COMPLETE, onMainUIXMLComplete);
			
			UIItems = new Object();
			
		}
		
		public function FriendUILoad()
		{
			var webroot:String = "file://C:\\Work\\Game\\SimLife\\";
			var urlRequest:URLRequest = new URLRequest(webroot + "Configuration\\FriendUIConfig.xml");
			urlLoader = new URLLoader();
			
			urlLoader.load(urlRequest);
			urlLoader.addEventListener(Event.COMPLETE, onFriendXMLComplete);
			
			FriendUIItems = new Object();
		}
		
		
		public function onMainUIXMLComplete(event:Event)
		{
			var myXML:XML;
			var className:String;
			var myBitMapData:BitmapData;
			var bitmapClass:Class;
			
			//var loader:URLLoader = event.target;
			if (urlLoader != null)
			{
				myXML = new XML(urlLoader.data);
				
				for each (var item:XML in myXML.item)
				{
					var typeString:String = item.Type;
					
					switch (typeString)
					{						
						case "Graphic":
							className = item.Resource;
							if (item.@id != null)
							{
								bitmapClass = getDefinitionByName(className) as Class;
							
								if (bitmapClass)
								{
									myBitMapData = new bitmapClass(item.Width, item.Height);
									UIItems[item.@id] = new Bitmap(myBitMapData);
									UIItems[item.@id].x = item.X;
									UIItems[item.@id].y = item.Y;
									addChild(UIItems[item.@id]);
								}
							}
							break;
							
						case "Gauge":
							if (item.@id != null)
							{
								var type:String = item.Resource;
								
								UIItems[item.@id] = new Gauge(type, item.Width, item.Height);
								UIItems[item.@id].x = item.X;
								UIItems[item.@id].y = item.Y;
								addChild(UIItems[item.@id]);
								
							}
							break;
							
						case "Button":
							className = item.Resource;
							if (item.@id != null)
							{
								UIItems[item.@id] = new UIButton(className, item.Width, item.Height);
								UIItems[item.@id].x = item.X;
								UIItems[item.@id].y = item.Y;
								addChild(UIItems[item.@id]);
							}
							break;
							
						case "Text":
							UIItems[item.@id] = new Shape();
							UIItems[item.@id].graphics.beginFill(0x000000);
							UIItems[item.@id].graphics.drawRect(item.X,item.Y,item.Width, item.Height);
							UIItems[item.@id].graphics.endFill();
							addChild(UIItems[item.@id]);
							break;
					}
					
				}//end for
				
			}//end if			
		}
		
		public function onFriendXMLComplete(event:Event)
		{
			var myXML:XML;
			
			//var loader:URLLoader = event.target;
			if (urlLoader != null)
			{
				myXML = new XML(urlLoader.data);
				
				for each (var item:XML in myXML.item)
				{
					var typeString:String = item.Type;
					
					switch (typeString)
					{						
						case "Graphic":
							var className:String = item.Resource;
							
							if (item.@id != null)
							{
								var bitmapClass:Class = getDefinitionByName(className) as Class;
							
								if (bitmapClass)
								{
									var myBitMapData:BitmapData = new bitmapClass(item.Width, item.Height);
									UIItems[item.@id] = new Bitmap(myBitMapData);
									UIItems[item.@id].x = item.X;
									UIItems[item.@id].y = item.Y;
									addChild(UIItems[item.@id]);
								}
							}
							break;
						case "Sprite":
							trace("enter Sprite");
							UIItems[item.@id] = new Shape();
							UIItems[item.@id].graphics.beginFill(0x000000);
							UIItems[item.@id].graphics.drawRect(item.X,item.Y,item.Width, item.Height);
							UIItems[item.@id].graphics.endFill();
							addChild(UIItems[item.@id]);
							break;
						case "Button":
							trace("enter Button");
							UIItems[item.@id] = new Shape();
							UIItems[item.@id].graphics.beginFill(0x000000);
							UIItems[item.@id].graphics.drawRect(item.X,item.Y,item.Width, item.Height);
							UIItems[item.@id].graphics.endFill();
							addChild(UIItems[item.@id]);
							break;
						case "Text":
							trace("enter Text");
							UIItems[item.@id] = new Shape();
							UIItems[item.@id].graphics.beginFill(0x000000);
							UIItems[item.@id].graphics.drawRect(item.X,item.Y,item.Width, item.Height);
							UIItems[item.@id].graphics.endFill();
							addChild(UIItems[item.@id]);
							break;
					}
					
				}//end for
				
			}//end if			
		}
		
		//add event listener for each button
		
	}//end class UILayer
	
	
}